﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BeerManagementSystem
{
    public partial class Payment : Form
    {
        private SQLConnector _sqlConnector;
        private Controller _controller;

        public Payment()
        {
            _sqlConnector = new SQLConnector();
            InitializeComponent();

            namesComboBox.AutoCompleteCustomSource.AddRange(_sqlConnector.GetUserNames()); 
            namesComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            namesComboBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            
        }

        private void Button_Click(object sender, EventArgs e)
        {
            string[] name = namesComboBox.Text.Split(' ');

            string pre = (name.Length < 3) ? name[0] : name[0] + " " + name[1];
            string last = (name.Length < 3) ? name[1] : name[2];

            if (_sqlConnector.GetUser(pre, last) == null)
                lastTransactionslistBox.Items.Add("Fehler: User nicht vorhanden.");
            else if (amountTextBox.Text != "")
            _sqlConnector.WriteCashTransaction(_sqlConnector.GetUser(pre, last), Convert.ToDecimal(amountTextBox.Text), commentTextBox.Text);

            lastTransactionslistBox.Items.Add(namesComboBox.Text + " / " + amountTextBox.Text + " / Account_alt: " + account_oldLabel.Text 
                + " / Account_neu: " + _sqlConnector.GetUser(pre, last).Account_beerbill.ToString() + " // " + commentTextBox.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (namesComboBox.Text != "")
            {
                string[] name = namesComboBox.Text.Split(' ');
                string pre = (name.Length < 3) ? name[0] : name[0] + " " + name[1];
                string last = (name.Length < 3) ? name[1] : name[2];
                User user = _sqlConnector.GetUser(pre, last);
                if (user != null) account_oldLabel.Text = "Account: "+ user.Account_beerbill;

                foreach (var cashTransaction in _sqlConnector.GetCashTransactions(user))
                {
                    lastTransactionslistBox.Items.Add(cashTransaction.ToString());
                }
                beerbillSumTextBox.Text = _sqlConnector.GetTotalBeerbill();

            }
        }


    }
}
